﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Configuration;

namespace Sinacor.OMM.Util
{
    /// <summary>
    /// Dumper configuration manager
    /// </summary>
    public static class UtilConfigurationManager
    {

        static Configuration.Dumper.DumperConfiguration _configurationInstance = null;
        static string _configFile;
        static Int32 _companyId;

        /// <summary>
        /// Company Id
        /// </summary>
        public static Int32 CompanyId
        {
            get 
            {
                if (_companyId == 0)
                {
                    if (string.IsNullOrEmpty(_configFile))
                        _configFile = Path.Combine(Path.GetDirectoryName(System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath), "Sinacor.OMM.DumpConfiguration.dll");

                    System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(_configFile);

                    if (config == null)
                        throw new ConfigurationErrorsException(string.Format("Configuration file '{0}' not found.", _configFile));

                    if (!Int32.TryParse(config.AppSettings.Settings["companyId"].Value, out _companyId))
                        throw new ConfigurationErrorsException("Company id configuration not found.");
                }

                return _companyId;
            }
            
        }

        /// <summary>
        /// Dumper configuration
        /// </summary>
        public static Configuration.Dumper.DumperConfiguration DumperConfiguration
        {
            get
            {
                if (_configurationInstance == null)
                    _configurationInstance = new OMM.Configuration.Dumper.DumperConfigurationReader(CompanyId).GetConfiguration();

                return _configurationInstance;
            }
        }

    }
}
